package cn.coisini.controller.v1;

import cn.coisini.api.admin.FileControllerApi;
import cn.coisini.model.common.annotation.RedisAccessLimit;
import cn.coisini.model.common.dtos.Result;
import cn.coisini.model.common.enums.ResultEnum;
import cn.coisini.model.common.log.annotation.Log;
import cn.coisini.model.common.log.enums.BusinessType;
import cn.coisini.service.FileUploadService;
import jakarta.servlet.http.HttpServletResponse;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.InputStream;
import java.util.List;

/**
 * @author xiangshaw
 * Description: 文件上传
 */
@CrossOrigin
@RestController
@RequiredArgsConstructor
public class FileController implements FileControllerApi {

    private final FileUploadService fileUploadService;

    // 本地单文件上传
    @Override
    @RedisAccessLimit(seconds = 60, maxCount = 30)
    @Log(title = "文件管理", businessType = BusinessType.INSERT)
    public Result<String> uploadFile(MultipartFile file) {
       return  fileUploadService.localUploadFile(file);
    }

    // 本地多文件上传
    @Override
    @RedisAccessLimit(seconds = 60, maxCount = 30)
    @Log(title = "文件管理", businessType = BusinessType.INSERT)
    public Result<List<String>> uploadFiles(MultipartFile[] files) {
        return fileUploadService.localUploadFiles(files);
    }

    // 阿里云单文件上传
    @Override
    @RedisAccessLimit(seconds = 60, maxCount = 30)
    @Log(title = "文件管理", businessType = BusinessType.INSERT)
    public Result<String> aliUploadFile(MultipartFile file){
        return fileUploadService.aliUploadFile(file);
    }

    // minio文件上传

    /**
     {
     "code": 200,
     "message": "操作成功",
     "data": [
     "Optional[http://192.168.31.16:9000/coisini-blog/bdc3f11d-5cc7-4187-a3f6-510c373c03f7_182318YFFsA.jpg?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=coisini.cn%2F20240827%2Fus-east-1%2Fs3%2Faws4_request&X-Amz-Date=20240827T040340Z&X-Amz-Expires=604800&X-Amz-SignedHeaders=host&X-Amz-Signature=b9c930b2406a9b9d0fa03700cd8fde68c9e982c4c7701d11d4cb909b080a1921]",
     "Optional[http://192.168.31.16:9000/coisini-blog/dc51bd79-2e06-4ca5-a87b-c677ad874621_233626oPfFn.jpg?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=coisini.cn%2F20240827%2Fus-east-1%2Fs3%2Faws4_request&X-Amz-Date=20240827T040340Z&X-Amz-Expires=604800&X-Amz-SignedHeaders=host&X-Amz-Signature=14bee9fe30160dbd897c15af679ed81c6492cdf04a34c24ab2315cfed56d8c14]"
     ]
     }
     */
    @Override
    @RedisAccessLimit(seconds = 60, maxCount = 30)
    public Result<List<String>> minioUploadFile(MultipartFile[] files) {
        return fileUploadService.minioUploadFile(files);
    }

    // minio文件预览
    @Override
    @RedisAccessLimit(seconds = 60, maxCount = 30)
    public Result<String> minioPreviewFile(String fileName) {
        return fileUploadService.minioPreviewFile(fileName);
    }

    // minio文件下载
    @Override
    @RedisAccessLimit(seconds = 60, maxCount = 30)
    public Result<InputStream> downloadFile(String fileName, HttpServletResponse response) {
        return fileUploadService.minioDownloadFile(fileName, response);
    }

    // minio文件删除
    @Override
    @RedisAccessLimit(seconds = 60, maxCount = 30)
    public Result<ResultEnum> minioDeleteFile(String fileName) {
        return fileUploadService.minioDeleteFile(fileName);
    }
}




